package com.hh.test.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Date Utility Class
 */
/**
 * @author Qing
 * 
 */
public class DateUtil {

	public static String dateToFullDateStr(Date date) {
		if (date == null)
			return "";
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		if (month.length() < 2)
			month = "0" + month;
		String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		if (day.length() < 2)
			day = "0" + day;
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		if (hour.length() < 2)
			hour = "0" + hour;
		String minute = String.valueOf(calendar.get(Calendar.MINUTE));
		if (minute.length() < 2)
			minute = "0" + minute;
		String second = String.valueOf(calendar.get(Calendar.SECOND));
		if (second.length() < 2)
			second = "0" + second;
		String millisecond = String.valueOf(calendar.get(Calendar.MILLISECOND));
		if (millisecond.length() == 1) {
			millisecond = "00" + millisecond;
		} else if (millisecond.length() == 2) {
			millisecond = "0" + millisecond;
		}
		return year + month + day + hour + minute + second + millisecond;
	}

	public static String dateToFullDateStr2(Date date) {
		if (date == null)
			return "";
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		if (month.length() < 2)
			month = "0" + month;
		String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		if (day.length() < 2)
			day = "0" + day;
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		if (hour.length() < 2)
			hour = "0" + hour;
		String minute = String.valueOf(calendar.get(Calendar.MINUTE));
		if (minute.length() < 2)
			minute = "0" + minute;
		String second = String.valueOf(calendar.get(Calendar.SECOND));
		if (second.length() < 2)
			second = "0" + second;
		String millisecond = String.valueOf(calendar.get(Calendar.MILLISECOND));
		if (millisecond.length() == 1) {
			millisecond = "00" + millisecond;
		} else if (millisecond.length() == 2) {
			millisecond = "0" + millisecond;
		}
		return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + "." + millisecond;
	}

	/**
	 * 将日期格式转化为具有[日期时间]的字符串格式
	 * 
	 * @param date
	 * @return 日期格式的字符串,如"2004-10-25 14:16"
	 */
	public static String dateToMiddleDateStr(Date date) {
		if (date == null)
			return "";
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		if (month.length() < 2)
			month = "0" + month;
		String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		if (day.length() < 2)
			day = "0" + day;
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		if (hour.length() < 2)
			hour = "0" + hour;
		String minute = String.valueOf(calendar.get(Calendar.MINUTE));
		if (minute.length() < 2)
			minute = "0" + minute;
		String second = String.valueOf(calendar.get(Calendar.SECOND));
		if (second.length() < 2)
			second = "0" + second;
		return year + "-" + month + "-" + day + " " + hour + ":" + minute;
	}
	// protected static final Logger log =
	// LoggerFactory.getLogger(DateUtil.class);

	// ---------yuyy begin----------
	/**
	 * 将日期格式转化为具有[日期]的字符串格式
	 * 
	 * @param date
	 * @return 日期格式的字符串,如"2004-10-25"
	 */
	public static String dateToShortDateStr(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		if (month.length() < 2)
			month = "0" + month;
		String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		if (day.length() < 2)
			day = "0" + day;
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		if (hour.length() < 2)
			hour = "0" + hour;
		return year + "-" + month + "-" + day;
	}

	/**
	 * 将日期格式转化为具有[时间]的字符串格式
	 * 
	 * @param date
	 * @return 时间格式的字符串,如"09:30:00"
	 */
	public static String dateToShortTimeStr(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		if (hour.length() < 2) {
			hour = "0" + hour;
		}
		String minute = String.valueOf(calendar.get(Calendar.MINUTE));
		if (minute.length() < 2) {
			minute = "0" + minute;
		}
		String second = String.valueOf(calendar.get(Calendar.SECOND));
		if (second.length() < 2) {
			second = "0" + second;
		}
		return hour + ":" + minute + ":" + second;
	}

	/**
	 * 将日期格式转化为具有[日期时间]的字符串格式
	 * 
	 * @param date
	 * @return 日期格式的字符串,如"2004-10-25 14:16:02"
	 */
	public static String dateToLongDateStr(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		if (month.length() < 2)
			month = "0" + month;
		String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		if (day.length() < 2)
			day = "0" + day;
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		if (hour.length() < 2)
			hour = "0" + hour;
		String minute = String.valueOf(calendar.get(Calendar.MINUTE));
		if (minute.length() < 2)
			minute = "0" + minute;
		String second = String.valueOf(calendar.get(Calendar.SECOND));
		if (second.length() < 2)
			second = "0" + second;
		return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
	}

	/**
	 * 
	 * @param date
	 * @return 日期格式的字符串,如"20041025 14:16:02"
	 */
	public static String dateToSimpleLongDateStr(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		if (month.length() < 2)
			month = "0" + month;
		String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		if (day.length() < 2)
			day = "0" + day;
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		if (hour.length() < 2)
			hour = "0" + hour;
		String minute = String.valueOf(calendar.get(Calendar.MINUTE));
		if (minute.length() < 2)
			minute = "0" + minute;
		String second = String.valueOf(calendar.get(Calendar.SECOND));
		if (second.length() < 2)
			second = "0" + second;
		return year + month + day + " " + hour + ":" + minute + ":" + second;
	}

	/**
	 * 将[日期]字符串转化为具有[日期时间]的日期格式
	 * 
	 * @param strn
	 *            日期格式的字符串,如"2004-10-25"
	 * @return
	 */
	public static Date shortDateStrToDate(String strn) {
		Date returnValue = null;
		if (strn != null) {
			DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
			try {
				returnValue = df.parse(strn);
			} catch (ParseException pe) {
			}
		}
		return returnValue;
	}

	/**
	 * @param strn
	 *            日期格式的字符串,如"20041025"
	 * @return
	 */
	public static Date shortSimpleDateStrToDate(String strn) {
		return shortDateStrToDate(shortDateToFullDate(strn));
	}

	/**
	 * 将[时间]字符串转化为具有[时间]的Date格式
	 * 
	 * @param strn
	 *            日期格式的字符串,如"9:30:00"，要兼容"9","9:30"的格式
	 * @return
	 */
	public static Date shortTimeStrToDate(String str) {
		Calendar c = Calendar.getInstance();
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (str != null && !str.equals("")) {
			String[] strList = str.split(":");
			switch (strList.length) {
			case 1:
				hour = Integer.parseInt(strList[0]);
				break;
			case 2:
				hour = Integer.parseInt(strList[0]);
				minute = Integer.parseInt(strList[1]);
				break;
			case 3:
				hour = Integer.parseInt(strList[0]);
				minute = Integer.parseInt(strList[1]);
				second = Integer.parseInt(strList[2]);
			}
			c.set(Calendar.HOUR_OF_DAY, hour);
			c.set(Calendar.MINUTE, minute);
			c.set(Calendar.SECOND, second);
		}
		return c.getTime();

	}

	/**
	 * 将[日期时间]字符串转化为具有[日期时间]的日期格式
	 * 
	 * @param strn
	 *            日期格式的字符串,如"2004-10-25 14:16:02"
	 * @return
	 */
	public static Date longDateStrToDate(String strn) {
		Date returnValue = null;
		if (strn != null) {
			DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.CHINA);
			try {
				returnValue = df.parse(strn);
			} catch (ParseException pe) {
			}
		}
		return returnValue;
	}

	/**
	 * yyyy年MM月dd日 hh时mm分ss秒
	 * 
	 * @param date
	 * @return
	 */
	public static String formatFullDateTimeString(Date date) {
		return new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒", Locale.CHINA).format(date);
	}

	/**
	 * yyyy年MM月dd日 hh时mm分
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDateTimeNoSecondString(Date date) {
		return new SimpleDateFormat("yyyy年MM月dd日 HH时mm分", Locale.CHINA).format(date);
	}

	/**
	 * hh时mm分ss秒
	 * 
	 * @param date
	 * @return
	 */
	public static String formatFullTimeString(Date date) {
		return new SimpleDateFormat("HH时mm分ss秒", Locale.CHINA).format(date);
	}

	/**
	 * yyyy年MM月dd日
	 * 
	 * @param date
	 * @return
	 */
	public static String formatFullDateString(Date date) {
		return new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA).format(date);
	}

	/**
	 * yyyMMdd
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDateString(Date date) {
		return new SimpleDateFormat("yyyyMMdd", Locale.CHINA).format(date);
	}

	/**
	 * @param strn
	 *            日期格式的字符串,如"20041025 14:16:02"
	 * @return
	 */
	public static Date longSimpleDateStrToDate(String strn) {
		return longDateStrToDate(longDateToFullDate(strn));
	}

	/**
	 * 将日期往前或者往后加几天或者几个月或者几年，或者几个小时或者几分钟或者几秒
	 * 
	 * @param strn
	 *            例如将日期往后加3天：Date date =
	 *            DateUtil.dateAdd(Calendar.DAY_OF_MONTH,-3,new Date())
	 *            例如将日期往前加3天：Date date =
	 *            DateUtil.dateAdd(Calendar.DAY_OF_MONTH,3,new Date())
	 * @return
	 */
	public static Date dateAdd(int field, int amount, Date date) {
		Calendar cal = new GregorianCalendar();
		cal.setTimeInMillis(date.getTime());
		cal.add(field, amount);
		date = new Date(cal.getTimeInMillis());
		return date;
	}

	/**
	 * 将日期格式转化为具有[日期]的字符串格式
	 * 
	 * @param date
	 * @return 日期格式的字符串,如"2004-10-25"
	 */
	public static String timestampToShortDateStr(Timestamp time) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTimeInMillis(time.getTime());
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		if (month.length() < 2)
			month = "0" + month;
		String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		if (day.length() < 2)
			day = "0" + day;
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		if (hour.length() < 2)
			hour = "0" + hour;
		return year + "-" + month + "-" + day;
	}

	/**
	 * 将日期格式转化为具有[日期时间]的字符串格式
	 * 
	 * @param date
	 * @return 日期格式的字符串,如"2004-10-25 14:16:02"
	 */
	public static String timestampToLongDateStr(Timestamp time) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTimeInMillis(time.getTime());
		String year = String.valueOf(calendar.get(Calendar.YEAR));
		String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		if (month.length() < 2)
			month = "0" + month;
		String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
		if (day.length() < 2)
			day = "0" + day;
		String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		if (hour.length() < 2)
			hour = "0" + hour;
		String minute = String.valueOf(calendar.get(Calendar.MINUTE));
		if (minute.length() < 2)
			minute = "0" + minute;
		String second = String.valueOf(calendar.get(Calendar.SECOND));
		if (second.length() < 2)
			second = "0" + second;
		return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
	}

	/**
	 * 将[日期]字符串转化为具有[日期时间]的日期格式
	 * 
	 * @param strn
	 *            日期格式的字符串,如"2004-10-25"
	 * @return
	 */
	public static Timestamp shortDateStrToTimestamp(String strn) {
		Timestamp timeStamp = null;
		if (strn != null) {
			DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
			try {
				Date date = df.parse(strn);
				timeStamp = new Timestamp(date.getTime());
			} catch (ParseException pe) {
			}
		}
		return timeStamp;
	}

	/**
	 * 将[日期时间]字符串转化为具有[日期时间]的日期格式
	 * 
	 * @param strn
	 *            日期格式的字符串,如"2004-10-25 14:16:02"
	 * @return
	 */
	public static Timestamp longDateStrToTimestamp(String strn) {
		Timestamp timeStamp = null;
		if (strn != null) {
			DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.CHINA);
			try {
				Date date = df.parse(strn);
				timeStamp = new Timestamp(date.getTime());
			} catch (ParseException pe) {
			}
		}
		return timeStamp;
	}

	/**
	 * 将日期往前或者往后加几天或者几个月或者几年，或者几个小时或者几分钟或者几秒
	 * 
	 * @param strn
	 *            例如将日期往后加3天：Timestamp date =
	 *            DateUtil.dateAdd(Calendar.DAY_OF_MONTH,-3,new Timestamp())
	 *            例如将日期往前加3天：Timestamp date =
	 *            DateUtil.dateAdd(Calendar.DAY_OF_MONTH,3,new Timestamp())
	 * @return
	 */
	public static Timestamp dateAdd(int field, int amount, Timestamp time) {
		Calendar cal = new GregorianCalendar();
		cal.setTimeInMillis(time.getTime());
		cal.add(field, amount);
		return new Timestamp(cal.getTimeInMillis());
	}

	// ---------yuyy end-----------

	/**
	 * 判断当前时间是否在某个时间点前
	 * 
	 * @param dateString
	 *            时间点字符串，格式不限
	 */
	public static boolean isDateBefore(String dateString) {
		try {
			DateFormat df = DateFormat.getDateTimeInstance();
			return isDateBefore(df.parse(dateString));
		} catch (ParseException e) {
			System.out.print(e.getMessage());
			return false;
		}
	}

	/**
	 * 判断当前时间是否在某个时间点后
	 * 
	 * @param dateString
	 *            时间点字符串，格式不限
	 */
	public static boolean isDateAfter(String dateString) {
		try {
			DateFormat df = DateFormat.getDateTimeInstance();
			return isDateBefore(df.parse(dateString));
		} catch (ParseException e) {
			System.out.print(e.getMessage());
			return false;
		}
	}

	/**
	 * 判断当前时间是否在某个时间点前
	 * 
	 * @param dateString
	 *            时间点字符串，格式不限
	 */
	public static boolean isDateBefore(Date date) {
		Date currDate = new Date();
		return currDate.before(date);
	}

	/**
	 * 判断当前时间是否在某个时间点后
	 * 
	 * @param dateString
	 *            时间点字符串，格式不限
	 */
	public static boolean isDateAfter(Date date) {
		Date currDate = new Date();
		return currDate.after(date);
	}

	/**
	 * 判断当前时间是否不在某个时间区内
	 * 
	 * @param leftDate
	 *            左边时间点
	 * @param rightDate
	 *            右边时间点
	 */
	public static boolean isDateBetween(Date leftDate, Date rightDate) {
		boolean afterLeft = isDateAfter(leftDate);
		boolean beforeRight = isDateBefore(rightDate);
		return afterLeft & beforeRight;
	}

	/**
	 * 20140101=2014-01-01
	 * 
	 * @param date
	 * @return
	 */
	public static String shortDateToFullDate(String date) {
		return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6);
	}

	public static String longDateToFullDate(String datetime) {
		return datetime.substring(0, 4) + "-" + datetime.substring(4, 6) + "-" + datetime.substring(6, 8)
				+ datetime.substring(8);
	}

	/**
	 * 2017-1-27 转 2017-01-27
	 * 
	 * @param datetime
	 * @return
	 */
	public static String shortDateToShortDate(String datetime) {
		String[] sl = datetime.split("-");
		String re = datetime;
		if (sl.length == 3) {
			if (sl[1].length() < 2) {
				sl[1] = "0" + sl[1];
			}
			if (sl[2].length() < 2) {
				sl[2] = "0" + sl[2];
			}
			re = sl[0] + "-" + sl[1] + "-" + sl[2];
		}
		return re;

	}

	public static long getDayOf(String time1, String time2) {
		long quot = 0;
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			Date date1 = ft.parse(time1);
			Date date2 = ft.parse(time2);
			quot = getDayOf(date1, date2);
		} catch (ParseException e) {
			// log.error(e.getMessage());
		}
		return quot;
	}

	/**
	 * 获取没有时间的日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDate(Date date) {
		String sDate = dateToShortDateStr(date);
		return shortDateStrToDate(sDate);
	}

	/**
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getDayOf(Date date1, Date date2) {
		long quot = 0;
		quot = date1.getTime() - date2.getTime();
		quot = quot / 1000 / 60 / 60 / 24;
		return quot;
	}

	public static double getDayOfD(Date date1, Date date2) {
		double quot = 0.0;
		quot = date1.getTime() - date2.getTime();
		quot = quot / 1000 / 60 / 60 / 24;
		return quot;
	}

	/**
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getYearOf(Date date1, Date date2) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date1);
		long year = calendar.get(Calendar.YEAR);
		long month = calendar.get(Calendar.MONTH) + 1;
		long day = calendar.get(Calendar.DATE);
		calendar.setTime(date2);
		year -= calendar.get(Calendar.YEAR);
		month -= calendar.get(Calendar.MONTH) + 1;
		day -= calendar.get(Calendar.DATE);
		if (day == 0 && month == 0)
			return year;
		else
			return 0;
	}

	public static String getAge(Date birthDay) {
		Calendar cal = Calendar.getInstance();

		if (cal.before(birthDay)) {
			throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
		}

		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

		cal.setTime(birthDay);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;// 多少年

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				// monthNow==monthBirth
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				// monthNow>monthBirth
				age--;
			}
		}

		return age + "";
	}

	public static String getLongTimeWIthNano(Timestamp d) {
		Date date = new Date(d.getTime());
		String time = dateToFullDateStr(date);
		time += "-" + d.getNanos();
		return time;
	}

	public static void main(String arg[]) {
		try {
			// Date b = DateUtil.longDateStrToTimestamp("2014-12-31 23:59:59");
			// Date a = DateUtil.longDateStrToTimestamp("2012-2-31 23:59:59");

			Date date1 = longSimpleDateStrToDate("20180224 10:00:00");
			Date date2 = longSimpleDateStrToDate("20180225 00:00:00");
			// System.out.println(getDayOf(date2, date1));
			System.out.println(date1.compareTo(date2));
			System.out.println(date1.after(date2));

			// String d = "2016-1-9";
			// System.out.println(shortDateToShortDate(d));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
